// PcmRender.cpp: implementation of the CPcmRender class.
//
//////////////////////////////////////////////////////////////////////

#include "PcmRender.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPcmRender::CPcmRender()
{

}

CPcmRender::~CPcmRender()
{

}

/*********************************************************************
*FILE:
*     		CSynThread.cpp
* Function:
*     		CSynThread()
*Input:
*		None
*Output:
*		None
*Brief:
*		Constructor
*Author:
*		Liuli
 *********************************************************************/


int CPcmRender::Init()
{
	WaveHeader.resize(m_Param.AudioBufferMaxCounter);
	
	int error = 0;

	
	if( !error )
	{		
		WAVEFORMATEX format;
		format.wFormatTag = WAVE_FORMAT_PCM;
		format.nChannels = m_Param.channel;
		format.nSamplesPerSec = m_Param.sample_rate;
		format.wBitsPerSample = m_Param.samplewidth;
		format.nBlockAlign  = m_Param.channel * m_Param.samplewidth / 8; //4;
		format.nAvgBytesPerSec =  m_Param.sample_rate * m_Param.samplewidth * m_Param.channel / 8;
		format.cbSize = 0 ; 
		if( (error=waveOutOpen( &handle , WAVE_MAPPER , &format , (DWORD)WaveCallBackProc , (DWORD)this , CALLBACK_FUNCTION) ) 
			!=  MMSYSERR_NOERROR )
		{
			hw_trace_msg( HW_TRACE_ERROR , "JAudioDevice::OpenOutput fail %d\n" , error );
			error = 4;
		}

	}
	return 1;
}
int CPcmRender::Render(Packet*pPacket)
{
		if(pPacket == NULL)
			return -1;

		Push(pPacket);

		if(m_)
	
			
}
int CPcmRender::close()

{
		return 1;
}
int CPcmRender::flush()
{
		return 1;
}

int CPcmRender::Push(Packet *p)
{

}

Packet* CPcmRender::Pop()
{

}

void CPcmRender::WaveOutputProc(HWAVEIN hwi, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
	if( uMsg == WOM_DONE )
	{
		CPcmRender* inst = (CPcmRender*)dwInstance;
		hw_enter_critical( inst->Critical );
		inst->SendDataToDevice(); ;
//		printf( "output ready %d %d\n" , inst->CompleteBlock , inst->BufferedBlock);
		hw_set_event(inst->Event);	
		hw_leave_critical( inst->Critical );
	}	
}

void CPcmRender::SendDataToDevice()
{
	
	waveOutUnprepareHeader( inst->handle , head , sizeof(WAVEHDR) );
	waveOutWrite( inst->handle , head , sizeof(WAVEHDR));
}
